<!DOCTYPE html>
<html style="height: 100%">
<head>
    <title>资源搜索</title>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="Expires" content="0">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-control" content="no-cache">
    <meta http-equiv="Cache" content="no-cache">
    <link href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/cerulean/bootstrap.min.css" rel="stylesheet" integrity="sha384-zF4BRsG/fLiTGfR9QL82DrilZxrwgY/+du4p/c7J72zZj+FLYq4zY00RylP9ZjiT" crossorigin="anonymous">
    <style type="text/css">
        body{
            height: 100%;background:center 100%;background-image: url('/torrent/assets/img/background.jpg') ;background-repeat :no-repeat;
        }

        div.wrap{
            height: 100%;width:100%;overflow-y:scroll;
        }
        div.content{
            text-align:center;display: none
        }

        table.main{
            min-width:800px;max-width:1500px; margin-bottom: 0px;margin:0 auto
        }

        .table > thead > tr > th{
            background-color:#99cdff;
        }
        .table >  tbody > tr:nth-of-type(odd) {
            background-color: #f9f9f9;
        }
        .table >  tbody > tr:nth-of-type(odd):hover {
             background-color: #E8E8E8;
         }
        .table > tbody > tr:nth-of-type(even) {
             background-color: #d9edf7;
         }
        .table > tbody > tr:nth-of-type(even):hover {
             background-color:#C4E3F3 ;
         }

        .table > tbody > tr >td.time{
            width:120px
        }
        .table > tbody > tr >td.download{
            width:120px
        }

        footer > ul{
            margin-top: 0px
        }

        div.loading{
            position:absolute;left: 50%; top: 50%;transform: translate(-50%, -50%) ;display:none
        }
    </style>
</head>
<body>
<div class="wrap">
    <div class="header"  id="navbar">
        <nav class="navbar navbar-default">
            <div class="container-fluid">
                <div class="navbar-header">
                    <span class="navbar-brand">大家在搜：</span>
                </div>

                <div class="collapse navbar-collapse">
                    <ul class="nav navbar-nav">
                        <li><a href="#" v-on:click.prevent="quickSearch($event)">铁甲城的卡巴里内</a></li>
                        <li><a href="#" v-on:click.prevent="quickSearch($event)">反叛的鲁路修</a></li>
                        <li><a href="#" v-on:click.prevent="quickSearch($event)">高达</a></li>
                        <li><a href="#" v-on:click.prevent="quickSearch($event)">collection</a></li>
                    </ul>
                    <form class="navbar-form navbar-left" role="search" id="form-search">
                        <div class="form-group">
                            <input type="text" class="form-control" placeholder="什么都能搜" v-model.trim="searchContent">
                        </div>
                        <button type="submit" class="btn btn-default" v-on:click.prevent="search">搜索</button>
                    </form>
<!--                    <ul class="nav navbar-nav navbar-right">
                        <li><a href="#">注册</a></li>
                        <li><a href="#">登录</a></li>
                    </ul>-->
                </div>
            </div>
        </nav>
    </div>
    <div class="container content" id="div-content" :style="styleContent">
        <table class="table table-hover main">
            <!--class="table table-striped table-hover style="filter: Alpha(Opacity=50, Style=3 ); ""background-color:#79f2ff;filter: Alpha(opacity=50) -->
            <thead>
            <tr >
                <th>资源描述</th>
                <th style="width:120px">日期</th>
                <th style="width:100px">下载</th>
            </tr>
            </thead>
            <tbody :items=items id="tbody-items">

            <template  v-for="item in items">
                <tr>
                    <td>{{ item.name}}</td>
                    <td class="time">{{ item.time}}</td>
                    <td class="download" is="item" :item=item></td>
                </tr>
            </template>
            </tbody>
        </table>

        <footer style=";margin:0 auto" id="footer-page" v-if="page.visible">
            <ul class="pagination">
                <li :class="page.leftClass"><a  href="#" v-on:click.prevent="lastPage">&laquo;</a></li>
                <li :class="page.rightClass"><a  href="#" v-on:click.prevent="nextPage">&raquo;</a></li>
                <!--class="active"-->
            </ul>
        </footer>

    </div>

    <!--;transform: translate(-50%, -50%) v-if="loading" position: absolute;left: 50%; top: 50% -->
    <div class="loading" id="loading" :style="loadingStyle">
        <img src="/torrent/assets/img/loading.gif" />
    </div>
</div>

<script src="https://code.jquery.com/jquery.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/vuex"></script>
<script>

    var loadingVue = new Vue({
        el: '#loading',
        data:{
            loadingStyle:{display:'none'}
        }
    })

    var divContent = new Vue({
        el: '#div-content',
        data: {
            styleContent: {
                display:'none'
            },
            items:[],
            page:{
                index:1,
                size:30,
                leftClass:"",
                rightClass:"",
                visible:false
            }
        },
        methods:{
            nextPage:function () {
                divContent.page.index++;
                searchContent();
            },
            lastPage:function () {
                divContent.page.index--;
                searchContent();
            }
        }
    })

   Vue.component('item',{
            props:['item'],
            template: "<td><a href='#' v-on:click.prevent='download(1)'>磁力链</a></td>",
            methods: {
                download:function(type){
                    console.log(this.item)
                    $.ajax({
                        url: "/torrent/torrent.json",    //请求的url地址
                        dataType: "json",   //返回格式为json
                        data: {url:this.item.href,type:type},    //参数值
                        type: "get",
                        success: function(result) {
                            if(result.status==1){
                                if(type==0){
                                    window.location.href=result.data
                                }else{
                                    console.log('url:'+result.data)
                                    window.open(result.data);
                                }
                            }else{
                                alert("异常"+result.msg);
                            }
                        },
                        error:function(result) {
                            console.log(result);
                            alert("请求出错");
                            //$.messager.alert("警告", "请求出错","error");
                        }
                    });
                }}
        }
    )

    var searchVue = new Vue({
        el: '#navbar',
        data: {searchContent:''},
        methods: {
            search:function () {
                searchContent();
            },
            quickSearch:function (event) {
                this.searchContent=event.target.text;
                divContent.page.index=1;
                searchContent();
            }
        }
    })


    function searchContent() {
        divLoading.loadingStyle.display='block';
        $.ajax({
            url: "/torrent/data.json",    //请求的url地址
            dataType: "json",   //返回格式为json
            data: {
                keyword:searchVue.searchContent,
                index:divContent.page.index,
                size:divContent.page.size
            },
            type: "get",
            success: function(result) {
                divLoading.loadingStyle.display='none';
                console.log(result);
                if(result.data.length!=0||divContent.page.index>1){
                    divContent.page.visible=true;

                    if(result.data.length<divContent.page.size){
                        divContent.page.rightClass='disabled';
                    }else{
                        divContent.page.rightClass='';
                    }

                    if(divContent.page.index==1){
                        divContent.page.leftClass='disabled';
                    }else{
                        divContent.page.leftClass='';
                    }
                }else{
                    divContent.page.visible=false;
                }
                divContent.items=result.data;
                divContent.styleContent.display='block';
            },
            error:function(result) {
                console.log(result);
                alert(result);
            }
        });
    }

    var download = function(type){
        console.log(this.item)
        $.ajax({
            url: "/torrent/torrent.json",    //请求的url地址
            dataType: "json",   //返回格式为json
            data: {url:this.item.href,type:type},    //参数值
            type: "get",
            success: function(data) {
                if(data.status=='1'){
                    if(type==0){
                        window.location.href=data.dataset
                    }else{
                        console.log('url:'+data.dataset)
                        window.open(data.dataset);
                    }
                }else{
                    alert("异常"+req.msg);
                }
            },
            error:function(data) {
                console.log(data);
                alert("请求出错");
            }
        });
    }

</script>
</body>
</html>